/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.contents;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import model.Content;
import model.ContentType;
import config.Debug;
import config.Settings;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class AnimatedGifBox extends ContentBox {
	private int id;
	private String filename = null;
	private int width;
	private int height;
	private Content content;
	private JLabel label = new JLabel();
	private boolean successfullyInitialized = false;

	public AnimatedGifBox() {
		super();
	}

	public AnimatedGifBox(Content c, int width, int height) {
		content = c;
		this.id = c.getId();
		this.filename = c.getContent();
		this.width = width;
		this.height = height;
		this.init();
	}

	public AnimatedGifBox(String filename, int width, int height) {
		this.filename = filename;
		this.width = width;
		this.height = height;
		this.init();
	}

	public void init() {
		this.setBounds(0, 0, this.width, this.height);
		this.setPreferredSize(new Dimension(this.width, this.height));
		this.setLayout(new BorderLayout());
		// continue if the icon file exists
		File imageFile = new File(Settings.getImageDirectory() + this.filename);
		if (!imageFile.exists()) {
			Debug.msg("image not found " + imageFile.toString());
			Debug.log("image not found " + imageFile.toString(), Debug.WARNING_LEVEL);
			// return;
		}
		// continue if the icon file fits into container
		ImageIcon icon = new ImageIcon(Settings.getImageDirectory() + this.filename);
		if (icon.getIconHeight() > this.height || icon.getIconWidth() > this.width) {
			Debug.log("image icon cannot be fit into container ", Debug.WARNING_LEVEL);
			// return;
		}

		label = new JLabel();
		label.setIcon(icon);
		this.add(label);
		this.setSuccessfullyInitialized(true);
	}

	public static void main(String[] args) {

		final AnimatedGifBox gif = new AnimatedGifBox(new Content("maintenance.gif", ContentType.GIF), 400, 300);
		// final JLabel gif = new JLabel("asdfasdf");
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				JFrame f = new JFrame("test Frame");
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.getContentPane().setLayout(new BorderLayout());
				f.getContentPane().add(gif);
				f.setPreferredSize(new Dimension(400, 300));
				f.pack();
				f.setVisible(true);
			}
		});
	}

	@Override
	public String getContent() {
		return this.filename;
	}

	@Override
	public void cleanUp() {
		this.removeAll();
		this.revalidate();
	}

	@Override
	public void reinitialize() {
	}

	@Override
	public void initialize() {
	}

	@Override
	public boolean isSuccessfullyInitialized() {
		return successfullyInitialized;
	}

	public void setSuccessfullyInitialized(boolean successfullyInitialized) {
		this.successfullyInitialized = successfullyInitialized;
	}

}